package com.goods.business.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Health;
import com.goods.common.model.system.User;
import com.goods.common.utils.JWTUtils;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import com.goods.system.mapper.UserMapper;
import org.apache.poi.util.Internal;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: Mr.Cao
 * @DateTime: 2021/11/10 23:35
 * BMW F750GS
 **/
@Service
public class HealthServiceImpl implements HealthService {
    @Autowired
    private HealthMapper healthMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * 检查今日是否打卡
     * */
    @Override
    public HealthVO isReport(HttpServletRequest request) {
        Long userId = this.getUserId(request);
        if (userId!=null){
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String nowDate = sdf.format(new Date());
                Example example = new Example(Health.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andLike("createTime",nowDate+"%");
                criteria.andEqualTo("userId",userId);
                Health health = healthMapper.selectOneByExample(example);
                if (health!=null){
                    //转Vo
                    HealthVO healthVO = new HealthVO();
                    BeanUtils.copyProperties(health,healthVO);
                    return healthVO;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    private Long getUserId(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if (!StringUtils.isEmpty(token)){
            String username = JWTUtils.getUsername(token);
            Example example = new Example(User.class);
            example.createCriteria().andEqualTo("username",username);
            User user = userMapper.selectOneByExample(example);
            return user.getId();
        }
        return null;
    }
    /**
     * 用户打卡
     * */
    @Override
    public void report(HealthVO healthVO, HttpServletRequest request) {
        Long userId = this.getUserId(request);
        if (userId!=null){
            //转换
            Health health = new Health();
            BeanUtils.copyProperties(healthVO,health);
            health.setUserId(userId);
            health.setCreateTime(new Date());
            healthMapper.insert(health);
        }
    }
    /**
     * 分页查询签到记录
     * */
    @Override
    public PageVO history(Map queryMap, HttpServletRequest request) {
        Long userId = this.getUserId(request);
        if (userId!=null){
            //分页查询
            String pageSize = (String) queryMap.get("pageSize");
            String pageNum = (String) queryMap.get("pageNum");
            Page<Object> page = PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
            Example example = new Example(Health.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("userId",userId);
            List<Health> healthList = healthMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(healthList)){
                //转Vo
                List<HealthVO> healthVOList = healthList.stream().map(health -> {
                    HealthVO healthVO = new HealthVO();
                    BeanUtils.copyProperties(health, healthVO);
                    return healthVO;
                }).collect(Collectors.toList());
                return new PageVO(page.getTotal(),healthVOList);
            }
        }
        return null;
    }
}
